﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;
using System.Web.SessionState;
using ProjectStudy.App_Code;

namespace ProjectStudy.Ajax
{
    /// <summary>
    /// Ajax_UserManage 的摘要说明
    /// </summary>
    public class Ajax_UserManage : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action=context.Request["action"];
            string result = "";
            switch (action)
            {
                case "GetUserList": result = GetUserList(context);
                    break;
                case "GetUserInfo": result = GetUserInfo(context);
                    break;
                case "UserActive": result = UserActiveOrNotActive(context);
                    break;
                case "UserLock": result=UserLockOrUnLock(context);
                    break;
                case "UserDel": result = UserDel(context);
                    break;
                case "UserGroupChange": result = UserGroupChange(context);
                    break;
            }
            context.Response.Write(result);
        }

        private string GetUserInfo(HttpContext context)
        {
            string sql = "select * from User2 where username=@username";
            SqlParameter ps = new SqlParameter("@username",SysSession.UserName);
            DataTable dt = DBAccess.ExecuteDataTable(sql, ps);
            return JsonConvert.SerializeObject(dt);
        }
        
        private string UserGroupChange(HttpContext context)
        {
            string[] username = context.Request["users"].Replace("[", "").Replace("]", "").Replace("\"", "").Split(',');
            string group=context.Request["group"];
            Dictionary<string, string> dic = new Dictionary<string, string>();
            string sql = "update User2 set [group]=@group where username=@username";
            string err = "";
            for (int i = 0; i < username.Length; i++)
            {
                SqlParameter[] ps = { 
                                    new SqlParameter("@group",group ),
                                    new SqlParameter("@username", username[i])
                                    }; 

                int a = DBAccess.ExecuteNonQuery(sql, ps);
                if (a <= 0)
                {
                    err = username[i] + ",";
                }
            }
            if (err.Length > 0)
            {
                err = err.Substring(0, err.Length);
                dic.Add("status", "updateErr");
                dic.Add("msg", err);
            }
            else
            {
                dic.Add("status", "success");
            }
            return JsonConvert.SerializeObject(dic);
        }

        private string UserDel(HttpContext context)
        {
            string[] username = context.Request["users"].Replace("[", "").Replace("]", "").Replace("\"", "").Split(',');
            Dictionary<string, string> dic = new Dictionary<string, string>();
            string sql = "delete from User2 where username=@username";
            string err = "";
            for (int i = 0; i < username.Length; i++)
            {
                SqlParameter ps = new SqlParameter("@username", username[i]);

                int a = DBAccess.ExecuteNonQuery(sql, ps);
                if (a <= 0)
                {
                    err = username[i] + ",";
                }
            }
            if (err.Length > 0)
            {
                err = err.Substring(0, err.Length);
                dic.Add("status", "delErr");
                dic.Add("msg", err);
            }
            else
            {
                dic.Add("status", "success");
            }
            return JsonConvert.SerializeObject(dic);
        }

        private string UserLockOrUnLock(HttpContext context)
        {
            string[] username = context.Request["users"].Replace("[", "").Replace("]", "").Replace("\"", "").Split(',');
            string isLock = context.Request["isLock"];
            Dictionary<string, string> dic = new Dictionary<string, string>();
            string sql = "update User2 set isLock=@islock where username=@username";
            string err = "";
            for (int i = 0; i < username.Length; i++)
            {
                SqlParameter[] ps = { 
                                    new SqlParameter("@islock",isLock),
                                    new SqlParameter("@username",username[i])
                                    };
                int a = DBAccess.ExecuteNonQuery(sql, ps);
                if (a <= 0)
                {
                    err = username[i] + ",";
                }
            }
            if (err.Length > 0)
            {
                err = err.Substring(0, err.Length);
                dic.Add("status", "updateErr");
                dic.Add("msg", err);
            }
            else
            {
                dic.Add("status", "success");
            }
            return JsonConvert.SerializeObject(dic);
        }

        private string UserActiveOrNotActive(HttpContext context)
        {
            string[] username = context.Request["users"].Replace("[", "").Replace("]", "").Replace("\"", "").Split(',');
            string isActive = context.Request["isActive"];
            Dictionary<string, string> dic = new Dictionary<string, string>();
            string sql = "update User2 set isActive=@isactive where username=@username";
            string err = "";
            for (int i = 0; i < username.Length; i++)
            {
                SqlParameter[] ps = { 
                                    new SqlParameter("@isactive",isActive),
                                    new SqlParameter("@username",username[i])
                                    };
                int a= DBAccess.ExecuteNonQuery(sql, ps);
                if (a <= 0)
                {
                    err = username[i] + ",";
                }
            }
            if (err.Length > 0)
            {
                err = err.Substring(0, err.Length);
                dic.Add("status", "updateErr");
                dic.Add("msg", err);
            }
            else
            {
                dic.Add("status","success");
            }
            return JsonConvert.SerializeObject(dic);

        }

        private string GetUserList(HttpContext context)
        {
            string group=context.Request["group"];
            string sql = "select * from User2";
            DataTable dt = new DataTable();
            if (group != "all")
            {
                sql = sql + " where [group]=@group";
                SqlParameter ps = new SqlParameter("@group", group);
                dt = DBAccess.ExecuteDataTable(sql, ps);
            }
            else
            {
                dt = DBAccess.ExecuteDataTable(sql);
            }
            return JsonConvert.SerializeObject(dt);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}